package library.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import library.pojo.User;
import library.service.LibraryService;

public class DeleteUserDialog extends JDialog {
	private User user;
	private JTextField username;
	private JTextField password;
	private JTextField name;
	private JTextField phone;
	private JTextField type;
	public DeleteUserDialog(String title,User user) {
		ImageIcon icon = new ImageIcon(MainJTPane.class.getResource("/title.png"));
		this.setIconImage(icon.getImage());
		this.user = user;
		this.setTitle(title);
		this.add(getUserPanel());
		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}

	private JPanel getUserPanel() {
		JPanel userPanel = new JPanel();
		username = new JTextField(15);
		password = new JTextField(15);
		phone = new JTextField(15);
		name = new JTextField(15);
		type = new JTextField(15);
		username.setEditable(false);
		password.setEditable(false);
		phone.setEditable(false);
		name.setEditable(false);
		type.setEditable(false);
		username.setText(user.getUsername());
		password.setText(user.getPassword());
		phone.setText(user.getPhone());
		name.setText(user.getName());
		type.setText(user.getType());
		JButton confirmButton = new JButton("确认删除");
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateActionPerformed(e);
			}
		});
		userPanel.add(new JLabel("    用户名"));
		userPanel.add(username);
		userPanel.add(new JLabel("        密码"));
		userPanel.add(password);
		userPanel.add(new JLabel("        姓名"));
		userPanel.add(name);
		userPanel.add(new JLabel("联系电话"));
		userPanel.add(phone);
		userPanel.add(new JLabel("用户类型"));
		userPanel.add(type);
		userPanel.add(confirmButton)  ;
		this.setSize(270, 220);
	return userPanel;
	}



	protected void updateActionPerformed(ActionEvent e) {
		LibraryService service = new LibraryService();
		service.deleteUser(user.getUsername());
		this.dispose();
		JOptionPane.showMessageDialog(null, "删除成功！");
	}
}
